//index.js
//获取应用实例
const utils = require('../../utils/util.js');
var app = getApp();
Page({
    data: {
        createFace: 'http://img.joyowo.com/formal/cms/banner/2016/12/82e4218da7b49447752f88999915559c.jpg',
        detailImg: 'http://img.joyowo.com/formal/cms/banner/2016/12/82e4218da7b49447752f88999915559c.jpg',
        radioItems:false,
        checkboxMax: 2,
        selectList:"",
        progress: 0,
        disabled: false,
        id:0,
        showModal: false,
        password:'',
        model:'',
        showDiv:'hideDiv'
    },
    radioChange: function (e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value);

        var radioItems = this.data.radioItems;
        for (var i = 0, len = radioItems.length; i < len; ++i) {
            radioItems[i].checked = radioItems[i].value == e.detail.value;
        }

        this.setData({
            radioItems: radioItems
        });
    },
    checkboxChange: function (e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value);


        let checkboxItems = this.data.checkboxItems;
        let checkboxMax = this.data.checkboxMax;

        let values = e.detail.value;

        if (checkboxMax < values.length) {
            values = values.splice(0, checkboxMax);


            console.log(values)

            for (let j = 0; j < checkboxItems.length; j++) {
                checkboxItems[j].checked = false;

                for (let i = 0; i < values.length; i++) {
                    if (checkboxItems[j].value == values[i]) {
                        checkboxItems[j].checked = true;
                    }
                }
            }

            console.log(checkboxItems)

        } else {
            for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
                checkboxItems[i].checked = false;

                for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
                    if (checkboxItems[i].value == values[j]) {

                        checkboxItems[i].checked = true;
                        break;
                    }
                }
            }
        }

        this.setData({
            checkboxItems: checkboxItems
        });

    },
    upload: function () {
        if (this.data.disabled) return;

        this.setData({
            progress: 0,
            disabled: true
        });
        _next.call(this);
    },

    onLoad:function (e) {

        console.log(e);

        // if(!options.id){options.id=27}
        wx.showLoading({
            title: '玩命加载中',
        });
        var $this = this;
        $this.data.id = e.id;

        //需要去请求数据
        utils.geturl({
            "handler": "Home",
            "method": "voteDetail",
            "param": '{"id":"' + $this.data.id + '"}',
            'token': app.globalData.token
        }, function (res) {

            console.log(res);


            if (res.code == 200) {
                $this.data.model = res.data.obj;

                var radioItems = res.data.obj.selectArr;
                // for (var i = 0, len = radioItems.length; i < len; ++i) {
                    // radioItems[i].checked = radioItems[i].value == e.detail.value;
                    // radioItems[i].checked = radioItems[i].value == e.detail.value;
                // }


                $this.setData({
                    "model": $this.data.model,
                    "radioItems": radioItems,
                    "showDiv": "showDiv",
                    "selectList":res.data.obj.selectList
                });
            }else{
                if (res.data.error == 12300){


                    $this.setData({
                        showModal: true
                    })
                }
            }
        });

    },
    showDialogBtn: function() {
        this.setData({
            showModal: true
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {
    },
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
        this.setData({
            showModal: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function (e) {
        console.log(e);

        wx.navigateBack();

        this.hideModal(e);


    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function (e) {

        console.log(e);

        var $this = this;

        var password = $this.data.password;

        //需要去请求数据
        utils.geturl({
            "handler": "Home",
            "method": "voteDetail",
            "param": '{"id":"' + $this.data.id + '","password":"'+password+'"}',
            'token': app.globalData.token
        }, function (res) {

            console.log(res);


            if (res.code == 200) {
                $this.data.model = res.data.obj;

                var radioItems = res.data.obj.selectArr;
                for (var i = 0, len = radioItems.length; i < len; ++i) {
                    radioItems[i].checked = radioItems[i].value == e.detail.value;
                }

                $this.setData({
                    "model": $this.data.model,
                    "radioItems": radioItems,
                    "showDiv": "showDiv",
                    "selectList":res.data.obj.selectList
                });
            }else{

                if (res.data.error == 12300){
                    $this.setData({
                        showModal: true
                    })
                }else{
                    setInterval(function () {
                        wx.navigateBack({});
                    }, 1500) //循环时间 这里是1秒
                }
            }
        });


        this.hideModal();
    },
    inputChange:function(e){
        console.log(e);

        var $this = this;

        $this.setData({
            password:e.detail.value
        })
    },
    showTopTips:function(e)
    {

        var $this = this;

        var checkNumList = $this.data.radioItems;

        var vote_id = $this.data.id;

        var i =0;

        var select = '';

        for (i;i<checkNumList.length;i++){

            if (checkNumList[i]['checked']==true){
                select = checkNumList[i]['value']
            }
        }

        console.log(select);

        utils.geturl({
            "handler": "Home",
            "method": "createPoll",
            "param": '{"id":"' + vote_id + '","select":"'+select+'"}',
            'token': app.globalData.token
        }, function (res) {

            console.log(res);


            if (res.code == 200) {
                wx.showToast({
                    title: '投票成功啦~',
                    icon: "success"
                });
            }

        })


    }
});
